#### Christopher M. Faulkner and Austin C. Doctor. "Rebel Fragmentation and Child Soldier Recruitment." International Studies Quarterly ####

library(car)
library(foreign)
library(readstata13)
library(readxl)
library(openxlsx)
library(xlsx)
library(dplyr)
library(plyr) 
library(lubridate)
library(gridExtra)
library(ggplot2)
library(maps)
library(mapdata)
library(mapproj)
library(reshape2)
library(graphics)
library(lattice)
library(gplots)
library(DataCombine)
library(glm.predict)
library(coefplot)
library(multiplot)
library(areaplot)



################################################ MANUSCRIPT FIGURES ####################################################
## FIGURE 1: WORLD HEAT MAP OF CHILD SOLDIERS
dta_data <- read.dta13("~/FORGE_CSDS_4_7_2020.dta"); colnames(dta_data)
agg_data <- aggregate(dta_data$dyadid, by=list(dta_data$cname, dta_data$Csdum), FUN= function(x) length(x))
agg_data1 <- agg_data[agg_data$Group.2 == "0", ]
agg_data2 <- agg_data[agg_data$Group.2 == "1", ]
agg_data3 <- merge(agg_data1, agg_data2, by = c("Group.1"), all=T)

agg_data3$Group.2.x <- 0
agg_data3$Group.2.y <- 1
agg_data3$x.x[is.na(agg_data3$x.x)] <- 0
agg_data3$x.y[is.na(agg_data3$x.y)] <- 0

agg_data3$ratio <- (agg_data3$x.y)/(agg_data3$x.x + agg_data3$x.y); as.data.frame(agg_data3)
agg_data3$total <- agg_data3$x.x + agg_data3$x.y; as.data.frame(agg_data3)

agg_data3$Group.1[14] <- "Congo"
agg_data3$Group.1[17] <- "Zaire"

WorldData <- map_data('worldLores') %>% filter(region != "Antarctica") %>% fortify
unique(WorldData$region)
df <- data.frame(region=c(), 
                 value=c(), 
                 stringsAsFactors=FALSE)

p <- ggplot() +
    geom_map(data = WorldData, map = WorldData,
                  aes(x = long, y = lat, group = group, map_id=region),
                  fill = "white", colour = "darkgrey", size=0.25) + 
    geom_map(data = agg_data3, map=WorldData,
                  aes(fill=ratio, map_id=Group.1),
                  colour="grey45", size=0.30) +
    coord_map("rectangular", lat0=0, xlim=c(-180,180), ylim=c(-60, 90)) +
    scale_fill_continuous(low="white", high="black", guide="colorbar", name="Proportion of Rebel Groups with Child Soldiers") +
    scale_y_continuous(breaks=c()) +
    scale_x_continuous(breaks=c()) +
    labs(fill=FALSE, title="", x="", y="") +
    theme_bw() + 
    theme(legend.position="bottom")
p 
 




## FIGURE 2: PREDICTED PROBABILITIES 
# (values taken from STATA margins command, shown in attached STATA .do and log files) 
plot(0, 0.7388864 , ylab= "", xlab="", ylim=c(0.6, 1), xlim=c(-0.3, 1.3), type="p", pch=0, axes=F, main="")
box()
axis(1, at=c(0,1), labels=c("Not Rebel Splinter Group", "Rebel Splinter Group"))
axis(2)
title(ylab="Predicted Probability of Child Soldier Recruitment", xlab="")
segments(0, 0.6723182 , 0, 0.8054545, lty=1, col="black")
lines(1, 0.8751117, type="p", pch= 0, col="black")
segments(1, 0.8133667, 1, 0.9368567, lty=1, col="black")









